//
//  PKLoginViewController.m
//  PianKe
//
//  Created by ybq on 15/12/24.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKLoginViewController.h"
#import "PKLoginView.h"
#import "PKRegisterViewController.h"

@interface PKLoginViewController () <UITextFieldDelegate>

@property (nonatomic, strong) PKLoginView *loginView;

@end

@implementation PKLoginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden = YES;
    [self initUI];
}



- (void)initUI {
    _loginView = [[PKLoginView alloc] initWithFrame:CGRectMake(0, 20, kWidth, kHeight)];
    [_loginView.backButton addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [_loginView.registerButton addTarget:self action:@selector(registerAction:) forControlEvents:(UIControlEventTouchUpInside)];
    _loginView.mailBox.textField.delegate = self;
    _loginView.mailBox.textField.tag = 100;
    _loginView.passWord.textField.delegate = self;
    _loginView.passWord.textField.secureTextEntry = YES;
    _loginView.passWord.textField.tag = 1000;
    [self.view addSubview:_loginView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
    if (self.view.frame.origin.y != 0) {
        CGRect newFrame = self.view.frame;
        newFrame.origin.y = 0;
        [UIView animateWithDuration:.3 animations:^{
            self.view.frame = newFrame;
        }];
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag == 100) {
        CGRect newFrame = self.view.frame;
        newFrame.origin.y = -40;
        [UIView animateWithDuration:.3 animations:^{
            self.view.frame = newFrame;
        }];
    } else if (textField.tag == 1000) {
        CGRect newFrame = self.view.frame;
        newFrame.origin.y = -70;
        [UIView animateWithDuration:.3 animations:^{
            self.view.frame = newFrame;
        }];
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    if (textField.tag == 100) {
        CGRect newFrame = self.view.frame;
        newFrame.origin.y = 0;
        [UIView animateWithDuration:.3 animations:^{
            self.view.frame = newFrame;
        }];
    } else if (textField.tag == 1000) {
        CGRect newFrame = self.view.frame;
        newFrame.origin.y = 0;
        [UIView animateWithDuration:.3 animations:^{
            self.view.frame = newFrame;
        }];
    }
    return YES;
}


- (void)registerAction:(UIButton *)sender {
    PKRegisterViewController *registerVC = [[PKRegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (void)backAction:(UIButton *)sender {
    [self.navigationController dismissViewControllerAnimated:self completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
